package com.lgc.mine.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import androidx.annotation.Keep
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.databinding.ObservableField
import com.lgc.mine.R
import com.lgc.mine.databinding.VItemSettingsBinding

/**
 * 自定义设置item组件控件
 */
class ItemSettingView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {
    private var itemBean=ItemSettingBean()
    private var obItemInfo= ObservableField(itemBean)
    init {
        //管理布局
        // val  view:View=LayoutInflater.from(context).inflate(R.layout.v_item_settings,this,true)
        VItemSettingsBinding.inflate(LayoutInflater.from(context), this, true).apply {
            info=obItemInfo //绑定数据
        }
        setBackgroundColor(Color.WHITE)
        //读取自定义的配置属性
        var attributes=context.obtainStyledAttributes(attrs, R.styleable.ItemSettingView).apply {
            //icon设置
            itemBean.iconRes=getResourceId(R.styleable.ItemSettingView_icon,R.drawable.ic_test_bird)
            val iconRGB=getColor(R.styleable.ItemSettingView_iconColor,0)
            itemBean.iconColor=iconRGB
            //title设置
            itemBean.title=getString(R.styleable.ItemSettingView_title)?:itemBean.title
            val titleRGB=getColor(R.styleable.ItemSettingView_titleColor,0)
            itemBean.titleColor=titleRGB
            //desc设置
            itemBean.desc=getString(R.styleable.ItemSettingView_desc)?:itemBean.desc
            val descRGB=getColor(R.styleable.ItemSettingView_descColor,0)
            itemBean.descColor=descRGB
            //箭头设置
            itemBean.arrowRes=getResourceId(R.styleable.ItemSettingView_arrow,R.drawable.ic_right_jiantou)
            var arrowRGB=getColor(R.styleable.ItemSettingView_arrowColor,resources.getColor(com.lgc.service.R.color.colorPrimaryText))
            itemBean.arrowColor=arrowRGB
        }
        //回收recycle
        attributes.recycle()
    }

    //点击事件
    fun setArrowRight(listener: View.OnClickListener?){
        itemBean.arrowRightListener=listener
    }

    //设置其它数据资源
    /**
     * 设置整个item对象的info
     */
    fun setInfo(info:ItemSettingBean){
        itemBean=info
        obItemInfo.set(info)
    }
    /**
     * 设置title
     */
    fun setTitle(title:String){
        itemBean.title=title

    }
    fun setTitleColor(color:Int){
        itemBean.titleColor=color
    }

    //拦截子view的响应事件
    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return hasOnClickListeners()
    }

    /**
     * 设置内容描述
     */
    fun setDesc(desc:String){
        itemBean.desc=desc
    }
    /**
     * 设置图标
     */
    fun setIcon(iconRes:Any){
        itemBean.iconRes=iconRes
    }

    /**
     * 设置图标
     */
    fun setArrow(arrowRes:Any){
        itemBean.arrowRes=arrowRes
    }

}

@Keep
data class ItemSettingBean(
    var iconRes: Any = R.drawable.ic_test_bird,
    var title: String = "Title标题",
    var desc: String = "描述描述",
    var titleColor: Int = com.lgc.service.R.color.colorPrimaryText,
    var descColor: Int = com.lgc.service.R.color.colorPrimaryText,
    var iconColor: Int = 0,
    var arrowColor: Int = 0,
    var arrowRes: Any = R.drawable.ic_right_jiantou
){
    //点击响应事件
    //item的子view的点击listener
    var iconListener: View.OnClickListener?=null
    var titleListener: View.OnClickListener?=null
    var descListener: View.OnClickListener?=null
    var arrowRightListener:View.OnClickListener?=null
}